import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Message from 'element-ui'
Vue.use(VueRouter)
const menus=store.state.menus
const index=  {
  path: '/index',
  name: 'index',
  component: () => import('../components/home/Index'),
  meta:{
    title:"首页"
  },
}
const list=[index]
if(menus != null){
  const menuList=store.state.menus;
  menuList.forEach(item=>{
    if(item.menuCode==="index"){
      return;
    }
    console.log("Item:",item)
    const route={
      path:'/'+item.menuCode,
      name:item.menuCode,
      component: () => import('../views/'+item.menuUrl),
      children:[
      ]
    }
    console.log("Views:",'../views/'+item.menuUrl)
    if(item.menuList.length>0){
      item.menuList.forEach(childItem=>{
        let child=null
        console.log("ChildItem:",childItem)
        if(childItem.menuCode==='finBaseAmountArchives'){
          child={
            path:'/'+item.menuCode+'/'+childItem.menuCode+"/:"+item.orderNo,
            name:childItem.menuCode+childItem.orderNo,
            meta:{
              title:childItem.menuName,
              url:childItem.menuUrl.replace(".vue",""),
              keepAlive:true,
            },
            component: () => import('../components/'+item.menuCode+'/'+childItem.menuUrl),
            children:[
            ]
          }
        }else{
          child={
            path:'/'+item.menuCode+'/'+childItem.menuCode,
            name:childItem.menuCode,
            meta:{
              title:childItem.menuName,
              url:childItem.menuUrl.replace(".vue",""),
              keepAlive:true,
            },
            component: () => import('../components/'+item.menuCode+'/'+childItem.menuUrl),
            children:[
            ]
          }
        }

        console.log("Components:",'../components/'+item.menuCode+'/'+childItem.menuUrl)
        route.children.push(child)
      })

    }
    list.push(route)
  })


  // list.forEach(rou=>{
  //   rou.children.forEach(ch=>{
  //     const add={
  //       path: ch.path+'/add',
  //       name:ch.name+"ProductForm",
  //       component:()=>import("../components"+rou.path+"/"+ch.meta.url+"FormComponent"),
  //       meta:{
  //         title:"数据添加"
  //       }
  //
  //     }
  //     console.log("meta:",ch.meta.url)
  //     console.log(add.path)
  //     console.log("../components"+rou.path+"/"+ch.meta.url+"FormComponent")
  //     rou.children.push(add)
  //     const edit={
  //       path: ch.path+'/edit/:id',
  //       name:rou.name+ch.name+"ProductForm",
  //       component:()=>import("../components"+rou.path+"/"+ch.meta.url+"FormComponent"),
  //       meta:{
  //         title:"数据修改"
  //       }
  //     }
  //     rou.children.push(edit)
  //   })
  // })
}
console.log("List:",list)
const routes = [
  {
    path: '/',
    name: 'home',
    redirect:"/index",
    meta:{
      title:"首页"
    },
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('../views/HomeView'),
    meta:{
      title:"首页"
    },
    children:list,
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
    meta:{
      title:"登录"
    },
  },
  {
    path: '/error',
    name: 'error',
    component: () => import('../views/Error.vue'),
    meta:{
      title:"出错啦！"
    },
  }

]
console.log("Routes:",routes)
// 缓存原型上的push函数
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace
// 给原型对象上的push指定新函数函数
VueRouter.prototype.push = function (location, onComplete, onAbort) {
  // 判断如果没有指定回调函数, 通过call调用源函数并使用catch来处理错误
  if (onComplete===undefined && onAbort===undefined) {
    return originPush.call(this, location, onComplete, onAbort).catch(() => {})
  } else { // 如果有指定任意回调函数, 通过call调用源push函数处理
    originPush.call(this, location, onComplete, onAbort)
  }
}

// replace函数
VueRouter.prototype.replace = function (location, onComplete, onAbort) {
  if (onComplete===undefined && onAbort===undefined) {
    return originReplace.call(this, location, onComplete, onAbort).catch(() => {})
  } else {
    originReplace.call(this, location, onComplete, onAbort)
  }
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.path === "/login") {
    window.localStorage.clear()
    next()
  }else{
    let userToken = localStorage.getItem('token')
    if (userToken == null || userToken === '' || userToken === undefined) {
      next('/login');
    } else {
      if(store.state.userInfo.first&&to.path!=="/index"){
        Message.MessageBox({
          type: "error",
          title: '警告',
          message: '首次登录，请重新设置登录密码'
        }).then(() => {
          window.location.href="/index"
          store.commit("setUpdatePasswordBoxShow",true)
          // router.push("/index")
          window.location.reload()
        });
      }
      else{
        if(to.matched && to.matched.length>2){
          to.matched.splice(1,to.matched.length -2)
        }
        next()
      }

    }
  }
})
export default router
